package com.library.common.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 时间格式
 */
@SuppressWarnings("ResourceType")
public class DateUtil {
    public static final String HHmmss = "HH:mm:ss";
    public static final String HHmm = "HH:mm";
    public static final String yyyyMMddHHmm = "yyyy年MM月dd日 HH:mm";
    public static final String yyyyMMddHHmmss = "yyyy年MM月dd日 HH:mm:ss";

    public static final String MMdd = "MM月dd日";
    public static final String MM__dd = "MM-dd";
    public static final String MM_dd = "MM/dd";
    public static final String yyyymm = "yyyy年MM月";
    public static final String yyyymmdd = "yyyy年MM月dd日";

    public static final String yyyy_MMddHHmm = "yyyy-MM-dd HH:mm";
    public static final String yyyy_MMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public static final String yyyyMMddHHmm2 = "yyyy/MM/dd HH:mm";
    public static final String yyyy_MM_dd = "yyyy-MM-dd";
    public static final String yyyy_MM = "yyyy-MM";


    public static long parseToDateLong(String s, String style) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        simpleDateFormat.applyPattern(style);
        Date date = null;
        if (s == null || s.length() < 5)
            return 0;
        try {
            date = simpleDateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    public static String parseToString(String s, String style) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        simpleDateFormat.applyPattern(style);
        String str = null;
        if (s == null || s.length() < 8)
            return null;
        try {
            Date date = simpleDateFormat.parse(s);
            str = simpleDateFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static String getNowTime(String style) {
        Date nowDate = new Date();
        Calendar now = Calendar.getInstance();
        now.setTime(nowDate);
        SimpleDateFormat formatter = new SimpleDateFormat(style);
        String str = formatter.format(now.getTime());
        return str;
    }


    public static String parseToMD(long time) {
        Calendar now = Calendar.getInstance();
        now.setTimeInMillis(time);
        SimpleDateFormat formatter = new SimpleDateFormat(MM__dd);
        String str = formatter.format(now.getTime());
        return str;
    }

    public static String getNextDate(int i, String style) {
        Date date = new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, i);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        SimpleDateFormat formatter = new SimpleDateFormat(style);
        String dateString = formatter.format(date);
        return dateString;
    }

    /**
     * 根据日期获取星期几
     *
     * @return
     */
    public static String dayForWeek(String dateStr, String style) {
        SimpleDateFormat formatYMD = new SimpleDateFormat(style);//formatYMD表示的是yyyy-MM-dd格式
        SimpleDateFormat formatD = new SimpleDateFormat("E");//"E"表示"day in week"
        Date d = null;
        String weekDay = "";
        try {
            d = formatYMD.parse(dateStr);//
            weekDay = formatD.format(d);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return weekDay;
    }

    /**
     * 计算时间戳与当前时间的差距
     *
     * @param aTimeStamp
     * @return
     */
    public static String getTimeDistance(long aTimeStamp) {
        String ret;
        long currenttime = new Date().getTime();
        long offset = currenttime - aTimeStamp;
        offset /= 1000;
        long year = offset / (3600 * 24 * 30 * 12);
        long month = offset / (3600 * 24 * 30);
        long day = offset / (3600 * 24);
        long hour = offset / 3600;
        long minute = offset / 60;
        if (year > 0) {
            ret = DateUtil.parseToString(aTimeStamp, yyyyMMddHHmm2);
        } else if (month > 0) {
            ret = DateUtil.parseToString(aTimeStamp, MM_dd);
        } else if (day > 1) {
            ret = DateUtil.parseToString(aTimeStamp, MM_dd);
        } else if (day == 1) {
            ret = "昨天";
        } else if (hour > 0) {
            ret = offset / 60 / 60 % 24 + "小时前";
        } else if (minute > 0) {
            ret = DateUtil.parseToString(aTimeStamp, HHmm);
        } else {
            ret = "刚刚";
        }
        return ret;
    }

    public static String parseToString(long curentTime, String style) {
        Calendar now = Calendar.getInstance();
        now.setTimeInMillis(curentTime);
        SimpleDateFormat formatter = new SimpleDateFormat(style);
        String str = formatter.format(now.getTime());
        return str;
    }

    /**
     * 总秒数  转换为   天、时、分、秒
     *
     * @return
     */
    public static String formatSeconds(long seconds) {
        String timeStr;
        if (seconds > 60) {
            long second = seconds % 60;
            long min = seconds / 60;
            timeStr = (String.valueOf(min).length() == 1 ? "0".concat(String.valueOf(min)) : String.valueOf(min)) + ":"
                    + (String.valueOf(second).length() == 1 ? "0".concat(String.valueOf(second)) : String.valueOf(second));
            if (min > 60) {
                min = (seconds / 60) % 60;
                long hour = (seconds / 60) / 60;
                timeStr = (String.valueOf(hour).length() == 1 ? "0".concat(String.valueOf(hour)) : String.valueOf(hour)) + ":"
                        + (String.valueOf(min).length() == 1 ? "0".concat(String.valueOf(min)) : String.valueOf(min)) + ":"
                        + (String.valueOf(second).length() == 1 ? "0".concat(String.valueOf(second)) : String.valueOf(second));
                if (hour > 24) {
                    hour = ((seconds / 60) / 60) % 24;
                    long day = (((seconds / 60) / 60) / 24);
                    timeStr = (String.valueOf(day).length() == 1 ? "".concat(String.valueOf(day)) : String.valueOf(day)) + "天"
                            + (String.valueOf(hour).length() == 1 ? "0".concat(String.valueOf(hour)) : String.valueOf(hour)) + ":"
                            + (String.valueOf(min).length() == 1 ? "0".concat(String.valueOf(min)) : String.valueOf(min)) + ":"
                            + (String.valueOf(second).length() == 1 ? "0".concat(String.valueOf(second)) : String.valueOf(second));
                }
            }
        } else {
            timeStr = (String.valueOf(seconds).length() == 1 ? "0".concat(String.valueOf(seconds)) : String.valueOf(seconds)) + "秒";
        }
        return timeStr;
    }

    /**
     * 总秒数  转换为 时、分、秒
     *
     * @return
     */
    public static String formatSeconds2(long seconds) {
        String timeStr;
        if (seconds > 60) {
            long second = seconds % 60;
            long min = seconds / 60;
            timeStr = (String.valueOf(min).length() == 1 ? "0".concat(String.valueOf(min)) : String.valueOf(min)) + ":" + (String.valueOf(second).length() == 1 ? "0".concat(String.valueOf(second)) : String.valueOf(second));
            if (min > 60) {
                min = (seconds / 60) % 60;
                long hour = (seconds / 60) / 60;
                timeStr = (String.valueOf(hour).length() == 1 ? "0".concat(String.valueOf(hour)) : String.valueOf(hour)) + ":" + (String.valueOf(min).length() == 1 ? "0".concat(String.valueOf(min)) : String.valueOf(min)) + ":" + (String.valueOf(second).length() == 1 ? "0".concat(String.valueOf(second)) : String.valueOf(second));
            }
        } else {
            timeStr = (String.valueOf(seconds).length() == 1 ? "0".concat(String.valueOf(seconds)) : String.valueOf(seconds));
        }
        return timeStr;
    }

    public static String getSomeDay(int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, day);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String str = formatter.format(calendar.getTime());
        return str;
    }

    public static Timestamp getWeeTimestamp() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date start = calendar.getTime();
        Timestamp timestamp = new Timestamp(start.getTime());
        return timestamp;
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int daysBetweenDay(Date date1, Date date2) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        long time1 = cal.getTimeInMillis();
        cal.setTime(date2);
        long time2 = cal.getTimeInMillis();
        long between_hours = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_hours));
    }
    //获取当月几号
    public static int getNowDate() {
        Calendar calendar = Calendar.getInstance();
        return  calendar.get(Calendar.DAY_OF_MONTH);
    }
}